package com.smart.imagecoupon.service;

import com.alibaba.fastjson.JSONArray;
import com.baidu.aip.imageclassify.AipImageClassify;
import com.smart.imagecoupon.bean.BaiduRep;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class SampleService {
    //设置APPID/AK/SK
    public static final String APP_ID = "23818409";
    public static final String API_KEY = "okXedKTDoRCeVd1AK2VrawdG";
    public static final String SECRET_KEY = "XZL47ERQyKaNICMO1PhftNSgv2uPq3MZ";

    public String getImageMessage(byte[] image) throws IOException {
        AipImageClassify client = new AipImageClassify(APP_ID, API_KEY, SECRET_KEY);
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);
        org.json.JSONObject res;
        try {
            res = client.advancedGeneral(image, new HashMap<String, String>());
            return res.get("result").toString();
        }catch (Exception e){
            return "{}";
        }
    }

    public BaiduRep getBaiduReq(String jsonString){
        try {
            return JSONArray.parseArray(jsonString, BaiduRep.class).get(0);
        }catch (Exception e){
            return null;
        }

    }
}
